package com.million.artservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.million.artservice.entity.Article;
import com.million.artservice.entity.vo.AddArticleVoInfo;
import com.million.artservice.entity.vo.ArticleVo;
import com.million.artservice.entity.vo.QueryArticleVo;
import com.million.artservice.service.ArticleService;
import com.million.commonutils.R;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章 前端控制器
 * </p>
 *
 * @author 十麓
 * @since 2022-04-17
 */
@RestController
@RequestMapping("/artservice/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //分页查询文章列表 返回Article列表
    @GetMapping("/queryArticlePage/{page}/{limit}")
    public R queryArticlePage (@ApiParam(name = "page", value = "当前页码", required = true)
                               @PathVariable("page") Long page,
                               @ApiParam(name = "limit", value = "每页记录数", required = true)
                               @PathVariable("limit") Long limit) {
        Page<Article> articlePage = new Page<>(page, limit);
        Map<String, Object> map = articleService.queryArticlePage(articlePage);
        return R.ok().data(map);
    }

    //查询最热文章 前8条
    @GetMapping("queryEightHotArticle")
    public R queryEightHotArticle () {
        List<Article> eightHotArticle = articleService.queryEightHotArticle();
        return R.ok().data("eightHotArticle", eightHotArticle);
    }

    //根据文章id 获取文章详情 编写sql
    @GetMapping("queryArticleById/{articleId}")
    public R queryArticleById (@PathVariable("articleId") String articleId) {
        ArticleVo articleVo = articleService.queryArticleById(articleId);
        return R.ok().data("articleVo", articleVo);
    }

    //============================================================
    //后台管理 文章管理  分页条件查询文章列表 返回ArticleVo列表
    @PostMapping("queryArticlePageConditions/{page}/{limit}")
    public R queryArticlePageConditions (@ApiParam(name = "page", value = "当前页码", required = true)
                                         @PathVariable("page") Long page,
                                         @ApiParam(name = "limit", value = "每页记录数", required = true)
                                         @PathVariable("limit") Long limit,
                                         @RequestBody QueryArticleVo queryArticleVo) {

        Page<ArticleVo> articlePage = new Page<>(page, limit);
        Page<ArticleVo> articleVoPage = articleService.queryArticlePageConditions(articlePage, queryArticleVo);
        return R.ok().data("articleVoPage", articleVoPage);
    }

    //添加文章 返回文章Id
    @PostMapping("addArticleInfo")
    public R addArticleInfo (@RequestBody AddArticleVoInfo addArticleVoInfo) {
        String articleId = articleService.addArticleInfo(addArticleVoInfo);
        return R.ok().data("articleId", articleId);
    }

    //根据文章Id 得到文章内容
    @GetMapping("getArticleInfo/{articleId}")
    public R getArticleInfo (@PathVariable("articleId") String articleId) {
        AddArticleVoInfo addArticleVoInfo = articleService.getArticleInfo(articleId);
        return R.ok().data("addArticleVoInfo", addArticleVoInfo);
    }

    //修改文章内容
    @PostMapping("updateArticleInfo")
    public R updateArticleInfo (@RequestBody AddArticleVoInfo addArticleVoInfo) {
       articleService.updateArticleInfo(addArticleVoInfo);
       return R.ok();
    }

    //根据文章id 删除文章信息
    @DeleteMapping("deleteArticleInfo/{articleId}")
    public R deleteArticleInfo(@PathVariable("articleId") String articleId){
        articleService.deleteArticleInfo(articleId);
        return R.ok();
    }


}

